from datetime import date
from time import mktime
import lib
import json

conn = lib.create_conn()


rate_list = {
    1:(1, 4),
    2:(1, 12),
    3:(1, 45),
    4:(1, 150),
    5:(1, 85),
    6:(1, 8),
    7:(1, 8),
    9:(1, 85),
    10:(1, 85),
    11:(1, 4),
    15:(1, 27),
    16:(1, 150),
    17:(1, 850),
    18:(1, 8),
    19:(1, 85),
    20:(1, 85),
    24:(1, 27),
    25:(1, 150),
    26:(1, 850),
    27:(1, 8),
    28:(1, 85)
}


def getResultUserPlay():
    mydate = date.today()
    current_date = int(mktime(mydate.timetuple()))
    userPlayArr = getUserPlayToday(1350061200)
    lastUserResult = []
    
    for u in userPlayArr:
        myDate = u['ngay']
        id_tinh = u['id_tinh']
        result = getResultNgay(id_tinh, 1350061200)
        for r in result:
            user = checkUserWin(u, json.dumps(r))
            lastUserResult.append(user)
    return lastUserResult
    

def getResultNgay(id_tinh, ngay):
    getResultStr = "CALL get_result_tinh('%s','%s')" % (id_tinh, ngay)
    info = lib.get_result_sql(conn, getResultStr)
    data = []
    for i in info :
        data.append(i['ketqua'])
    return data

def getUserPlayToday(myDate):
    
    userPlay = []
    mydate = date.today()
    current_date = int(mktime(mydate.timetuple()))
    getCardQuery = "CALL get_card_play('%s')" % myDate
    cardPlay = lib.get_result_sql(conn, getCardQuery)
    for c in cardPlay:
        buf = {}
        buf['id_user'] = c['id_user']
        buf['id_type'] = c['id_type']
        buf['id_giai'] = c['id_giai']
        buf['id_tinh'] = c['id_tinh']
        buf['play_card'] = c['play_card']
        buf['ngay'] = c['ngay']
        buf['price'] = c['price']
        buf['status'] = c['status']
        userPlay.append(buf)
    return userPlay

def getResultArr(ketquas):
    print ketquas
    resultArr = []
    resultLoad = json.loads(ketquas)
    for i in resultLoad:
        arrRes = i['result']
        resultArr += arrRes
    return resultArr



def getPlayArr(condanhs):
    playLoad = json.loads(condanhs)
    playArr = playLoad
    return playArr


def checkUserWin(userPlay, ketquas):
    
    print ketquas
    id_type = userPlay['id_type']
    condanhs = userPlay['play_card']
    resultArr = getResultArr(json.loads(ketquas))
    playArr = getPlayArr(condanhs)
    arr2Last = []
    buf = {}
    buf['user'] = userPlay['id_user']
    buf['price'] = userPlay['price']
    buf['ngay'] = userPlay['ngay']
    buf['loai_choi'] = userPlay['id_type']
    buf['con_thang'] = []
    for r in resultArr:
        res = int(r[-2:])
        arr2Last.append(res)  
    print arr2Last
    print playArr
    buf['tyle'] = rate_list.get(id_type, (1, 1)) 
      
    if id_type == 1:
        buf['status'] = 1 if set(playArr) & set(arr2Last) else 2
        a = set(playArr) & set(arr2Last)
        for i in a:
            mybuf = {}
            mybuf['con'] = i
            mybuf['num_con'] = arr2Last.count(i)
            buf['con_thang'].append(mybuf)
    elif id_type in [2, 15, 24]:
        buf['status'] = 1 if len(set(playArr) & set(arr2Last)) == 2 else 2
    elif id_type in [3, 16, 25]:
        buf['status'] = 1 if len(set(playArr) & set(arr2Last)) == 3 else 2
    elif id_type in [4, 17, 26]:
        buf['status'] = 1 if len(set(playArr) & set(arr2Last)) == 4 else 2
    elif id_type in [11, 18, 20]:
        buf['status'] = 1 if set(playArr) & set(arr2Last) else 2
        a = set(playArr) & set(arr2Last)
        for i in a:
            mybuf = {}
            mybuf['con'] = i
            mybuf['num_con'] = arr2Last.count(i)
            buf['con_thang'].append(mybuf)
    elif id_type in [5, 10,28]:
        interaction = False
        dbResult = resultArr[0]
        res = int(dbResult[-2:])
        for p in playArr:
            if p == res:
                buf['status'] = 1
                interaction = True
                break
                break
        if not interaction:
            buf['status'] = 2
    elif id_type == 6:
        interaction = False
        dbResult = resultArr[0]
        kecuoi = int(dbResult[-2])
        for p in playArr:
            if p == kecuoi:
                buf['status'] = 1
                interaction = True
                break
                break
        if not interaction:
            buf['status'] = 2
    elif id_type in [7,27]:
        interaction = False
        dbResult = resultArr[0]
        cuoi = int(dbResult[-1])
        for p in playArr :
            if p == cuoi:
                buf['status'] = 1
                interaction = True
                break
                break
        if not interaction:
            buf['status'] = 2
    elif id_type in [9, 19]:
        import pdb
        pdb.set_trace()
        interaction = False
        dbResult = resultArr[ len(resultArr) - 1 ]
        r = int(dbResult[-2:])
        for p in playArr:
            if r == p:
                buf['status'] = 1
                interaction = True
                break
                break
        if not interaction:
            buf['status'] = 2
    return json.dumps(buf)


if __name__ == "__main__" :
    user = getResultUserPlay()
    for u in user:
        print u
